package sorting;

import algUtils.StopWatch;
import edu.princeton.cs.algs4.StdRandom;

public class SortCompare {
    public static double time(String alg, Double[] a) {
        StopWatch timer = new StopWatch();
        if ("Selection".equals(alg)) Selection.sort(a);
        else if ("Insertion".equals(alg)) Insertion.sort(a);
        else if ("Shell".equals(alg)) Shell.sort(a);
        return timer.costTime();
    }

    public static double timeRandomInput(String alg, int N, int T) {
        //使用算法alg将T个长度为N的数组排序
        double total = 0.0;
        Double[] a = new Double[N];
        for (int t = 0; t < T; t++) {
            for (int i = 0; i < N; i++) {
                a[i] = StdRandom.uniform();
            }
            total += time(alg, a);
        }
        return total;
    }

    public static void main(String[] args) {
        String alg1 = args[0];
        String alg2 = args[1];
        int N = Integer.parseInt(args[2]);
        int T = Integer.parseInt(args[3]);
        double t1 = timeRandomInput(alg1, N, T);
        double t2 = timeRandomInput(alg2, N, T);
        System.out.println("t1:"+t1);
        System.out.println("t2:"+t2);
        System.out.printf("For %d random Doubles\n", N);
        if (t1 < t2) {
            System.out.printf("%s is %.2f times faster than %s\n", alg1, t2 / t1, alg2);
        }
        else {
            System.out.printf("%s is %.2f times faster than %s\n", alg2, t1 / t2, alg1);
        }
    }
}
